# Maintainer: David Runge <dvzrv@archlinux.org>

_name=imagick
pkgbase=php-imagick
pkgname=('php-imagick' 'php-legacy-imagick')
pkgver=3.7.0
pkgrel=3
pkgdesc="PHP extension to create and modify images using the ImageMagick library"
arch=(loong64 x86_64)
url="https://github.com/imagick/imagick"
license=(PHP)
depends=(imagemagick ttf-font)
makedepends=(librsvg php php-legacy)
checkdepends=(ttf-dejavu)
source=(
  "${pkgbase}-${pkgver}.tar.gz::https://github.com/${_name}/${_name}/archive/refs/tags/${pkgver}.tar.gz"
  "${_name}.ini"
)
sha512sums=('67cf7e76ee59dcf2fbb6230956344033022ea2b93f82fcdb949998638ce9990004948fddf13e7f4649b2115ce6f402a1eb4f95aa36a3b6a26f3f016b0e958bc3'
            '1463505bd6b2572e21f6bbc242c2e0bf8b881b839ac38e38c230f09b4bf4c4698cafba1a026da8f615ee2b2980ab74dc68284afd70bb732db6fb70b5efba2bfc')
b2sums=('3f8b94ca0619edee15de9aaf21ad3bf2896a072223b1eed70bfc8e47ef36fa766f370e801ae6b4300840a76e71d926b36cdbe7459df75804362ab4079eb2f56c'
        '291d68f50a2c173b857c0f132e1874f0682da3c7176f67b946a19a68256ab19f56a234bafa16711c3f8ef26c4bc7df04ea8afdecbcb984820b9bf3fd2a135edb')

prepare() {
  mv -v "${_name}-${pkgver}" "${pkgbase}-${pkgver}"
  # setting package version: https://bugs.archlinux.org/task/64185
  sed -e "s/@PACKAGE_VERSION@/${pkgver}/" \
      -i "${pkgbase}-${pkgver}/php_imagick.h" "${pkgbase}-${pkgver}/package.xml"

  cp -av "${pkgbase}-${pkgver}" "${pkgname[1]}-${pkgver}"
  (
    cd "$pkgname-${pkgver}"
    phpize
  )
  (
    cd "${pkgname[1]}-${pkgver}"
    phpize-legacy
  )
}

build() {
  (
    cd "${pkgbase}-${pkgver}"
    ./configure --prefix=/usr
    make
  )
  (
    cd "${pkgname[1]}-${pkgver}"
    ./configure --prefix=/usr
    make
  )
}

check() {
  (
    cd "${pkgbase}-${pkgver}"
    export NO_INTERACTION="true"
    make -k test
  )
  (
    cd "${pkgname[1]}-${pkgver}"
    export NO_INTERACTION="true"
    make -k test
  )
}

package_php-imagick() {
  depends+=(php)
  backup=("etc/php/conf.d/${_name}.ini")

  cd "${pkgname}-${pkgver}"
  make INSTALL_ROOT="$pkgdir/" install
  install -vDm 644 "../${_name}.ini" -t "${pkgdir}/etc/php/conf.d/"
  install -vDm 644 {ChangeLog,CREDITS,README.md} -t "${pkgdir}/usr/share/doc/${pkgname}/"
  install -vDm 644 examples/*.php -t "${pkgdir}/usr/share/doc/${pkgname}/examples"
}

package_php-legacy-imagick() {
  depends+=(php-legacy)
  backup=("etc/php-legacy/conf.d/${_name}.ini")

  cd "${pkgname}-${pkgver}"
  make INSTALL_ROOT="$pkgdir/" install
  install -vDm 644 "../${_name}.ini" -t "${pkgdir}/etc/php-legacy/conf.d/"
  install -vDm 644 {ChangeLog,CREDITS,README.md} -t "${pkgdir}/usr/share/doc/${pkgname}/"
  install -vDm 644 examples/*.php -t "${pkgdir}/usr/share/doc/${pkgname}/examples"
}
